package com.example.blogdemo1.controller;

/*
 * @author zzr
 * @date: 2025/10/05  19:42
 * @description:
 */

import com.example.blogdemo1.common.constants.Constants;
import com.example.blogdemo1.common.model.BlogInfo;
import com.example.blogdemo1.common.utils.JWTUtils;
import com.example.blogdemo1.service.BlogService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    /**
     * 获得博客列表
     * @return
     */
    @RequestMapping("/getList")
    public List<BlogInfo> getList() {

        return blogService.getList();

    }

    /**
     * 根据博客 id 获得博客内容
     * @param blogId
     * @return
     */
    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(@NotNull Integer blogId,HttpServletRequest request) {
        log.info("getBlogDetail,blogId:{}",blogId);
        return blogService.getBlogDetail(blogId,request);
    }

    /**
     * 添加博客
     * @param blogInfo
     * @param request
     * @return
     */
    @RequestMapping("/add")
    public boolean addBlog(@RequestBody BlogInfo blogInfo, HttpServletRequest request) {
        log.info("添加图书，blogInfo：{}", blogInfo);
        return blogService.insertBook(blogInfo, request);
    }

    /**
     * 更新博客
     * @param blogInfo
     * @return
     */
    @RequestMapping("/update")
    public Boolean update(BlogInfo blogInfo) {
        log.info("更新博客：blogInfo:{}",blogInfo);
        return blogService.update(blogInfo);
    }

    @RequestMapping("/delete")
    public Boolean delete(Integer blogId) {
        log.info("删除博客，blogId:{}",blogId);
        return blogService.delete(blogId);
    }
}
